package cn.tianyigps.tianyigpsactivetask.customer;

import cn.tianyigps.tianyigpsactivetask.assign.ActiveStatus;
import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpskafka.task.customer.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @Author: WEI.YUAN
 * @Descirption:
 * @Date: 2019/4/8_17:49
 */
@Component
public class ActiveCustomer extends Customer {
    @Autowired
    ActiveStatus activeStatus;

    @Override
    public boolean checkTask(Task task) {
        if (CollectionUtils.isEmpty(task.getParams())) {
            return false;
        }
        //task的TaskType不能为空
        if (StringUtils.isEmpty(task.getTaskType())) {
            return false;
        }
        //newxtTask不能为空
        if (task.getNextTask() == null) {
            return false;
        }
        Task nextTask = task.getNextTask();
        //nextTask的TaskType不能为空
        if (StringUtils.isEmpty(nextTask.getTaskType())) {
            return false;
        }
        //nextTask的callback不能为空
        if (!nextTask.getParams().containsKey("callback") || StringUtils.isEmpty(nextTask.getParams().get("callback"))) {
            return false;
        }
        //id不能为空
        if (StringUtils.isEmpty(task.getId())) {
            return false;
        }
        Map<String, Object> equipmentIdsMap = task.getParams();
        if (!equipmentIdsMap.containsKey("equipmentIds") || equipmentIdsMap.get("equipmentIds") == null) {
            return false;
        }
        return true;
    }


    @Override
    public void consume(Task task) {
        activeStatus.statusTask(task);
    }
}
